package com.hamster.crmpre.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.mapper.*;
import com.hamster.crmpre.pojo.*;
import com.hamster.crmpre.pojo.dto.BigCompanyRecordDTO;
import com.hamster.crmpre.pojo.dto.SmallCompanyRecordDTO;
import com.hamster.crmpre.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 定时任务执行的类
 * @author Tarry
 * @create 2019/12/31 9:18
 */

@Component
public class Task {
    @Autowired
    private CustomerCourseTaskMapper customerCourseTaskMapper;
    @Autowired
    private SchemeEmployeeMapper schemeEmployeeMapper;
    @Autowired
    private SchemeMapper schemeMapper;
    @Autowired
    private LeverageMapper leverageMapper;
    @Autowired
    private LeveragePlanInformationMapper leveragePlanInformationMapper;
    @Autowired
    private PersonalMessageMapper personalMessageMapper;
    @Autowired
    private CompanyAccountMapper companyAccountMapper;
    @Autowired
    private LeverageConfigMapper leverageConfigMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeePerformanceMapper employeePerformanceMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private PersonalMoneyMapper personalMoneyMapper;
    @Autowired
    private BigCompanyRecordMapper bigCompanyRecordMapper;
    @Autowired
    private SmallCompanyRecordMapper smallCompanyRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;
    /**
     * 定时执行结束参课任务
     */
    @Scheduled(cron = "0 0 0 * * ? ")
    public  void courseTask(){
        customerCourseTaskMapper.updateEndTask(new Date());
    }

    /**
     * 定时结束 执行方案
     */
    @Scheduled(cron = "0 0 0 * * ? ")
    @Transactional
    public void scheme(){
        Date endDate = new Date();
        schemeEmployeeMapper.updateEndScheme(endDate);
        schemeMapper.updateEndScheme(endDate);
    }

    /**
     * 系统定时结束 杠杆计划
     */
    @Scheduled(cron = "0 0 0 * * ? ")
    @Transactional
    public void  LeveragePlan(){
        //系统 自动查询数据库，找到需要结算的账单
        Date date = new Date();
        QueryWrapper<LeveragePlanInformation> leveragePlanInformationQueryWrapper = new QueryWrapper<>();
        leveragePlanInformationQueryWrapper.eq("state","0").eq("verify","1");
        leveragePlanInformationQueryWrapper.le("end_time",date);
        List<LeveragePlanInformation> list = leveragePlanInformationMapper.selectList(leveragePlanInformationQueryWrapper);
        for (LeveragePlanInformation leveragePlanInformation : list) {
            //完成百分比
            Double percentage = leveragePlanInformation.getActualAmount() / leveragePlanInformation.getTargetAmount();
            if (percentage < 0.8){
                //退还客户本金

                //公司总账减去 本金
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划未达标80%，退还本金");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-leveragePlanInformation.getPrincipal());//减去本金
                companyAccount.setContent("负责员工"+leveragePlanInformation.getEmployeeName()+"客户:"+
                        leveragePlanInformation.getCustomerName()
                        +"杠杆计划:"+leveragePlanInformation.getLeverageTitle()+"未达标退还："+
                        leveragePlanInformation.getPrincipal());//记账内容
                companyAccountMapper.insert(companyAccount);


                //通知财务一声
                Notice notice = new Notice();
                notice.setContent("客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone()+
                        "   杠杆计划："+leveragePlanInformation.getLeverageTitle()+"未达到80%，需退还客户本金："+leveragePlanInformation.getPrincipal()+"元");
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId("财务部");
                personalMessage.setReceiverName("财务部");
                personalMessage.setDepartment("财务部");
                personalMessageMapper.insert(personalMessage);

                //通知发起杠杆计划的人
                PersonalMessage personalMessage2 = new PersonalMessage();
                personalMessage2.setMessageType("通知消息");
                personalMessage2.setSenderId("系统");
                personalMessage2.setSenderName("系统");
                personalMessage2.setMessageLink(notice.getId());
                personalMessage2.setCreateTime(new Date());
                personalMessage2.setState("0");//消息状态 0未读
                personalMessage2.setReceiverId(leveragePlanInformation.getEmployeeId());
                personalMessage2.setReceiverName(leveragePlanInformation.getEmployeeName());

                personalMessageMapper.insert(personalMessage);
                return;
            }
            //查询该杠杆计划 对象
            String leverageType = leveragePlanInformation.getLeverageType();//杠杆计划的id
            Leverage leverage = leverageMapper.selectById(leverageType);//杠杆计划对象

            //1.查询各组提成比例
            LeverageConfig leverageConfig = leverageConfigMapper.selectById("1");

            //3.如果已完成，设置状态和完成进度
            if(percentage >= 0.8 && percentage<1){
                //2.查询对应各组的人员，和 对应的 提成比例
                //（本金 - 成本） *  百分比
                Double unqualified = leverage.getUnqualified();
                Double money = (leveragePlanInformation.getPrincipal() - leveragePlanInformation.getCost()) * unqualified;
                //原始服务经理id、[json数组]
                List<String> originalManagerList = JsonUtils.parseList(leveragePlanInformation.getOriginalManager(), String.class);
                Double  originalManagerMoney =money * leverageConfig.getOriginalPercent() / originalManagerList.size();
                for (String id : originalManagerList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(originalManagerMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("杠杆计划提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                        personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(id);//个人id【流水账主人】
                        personalMoney.setMoney(originalManagerMoney);
                        personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-originalManagerMoney);//收入金额
                    companyAccount.setContent("原始服务经理："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+originalManagerMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //微信组-总负责人id、[json数组]
                Double  wxGroupMoney =money * leverageConfig.getWxGroupPercent();
                List<String> wxGroupResponsibleList = JsonUtils.parseList(leveragePlanInformation.getWxGroupResponsible(), String.class);
                Double  wxGroupResponsibleMoney =wxGroupMoney * leverageConfig.getWxGroupPrimary() / wxGroupResponsibleList.size();
                for (String id : wxGroupResponsibleList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(wxGroupResponsibleMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("杠杆计划提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                        personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(id);//个人id【流水账主人】
                        personalMoney.setMoney(wxGroupResponsibleMoney);
                        personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-wxGroupResponsibleMoney);//收入金额
                    companyAccount.setContent("微信组总负责人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+wxGroupResponsibleMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }


                //微信组-参与人id、[json数组]
                List<String> wxGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getWxGroupParticipate(), String.class);
                Double  wxGroupParticipateMoney =wxGroupMoney * leverageConfig.getWxGroupSecondary() / wxGroupParticipateList.size();
                for (String id : wxGroupParticipateList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(wxGroupParticipateMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("杠杆计划提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                        personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(id);//个人id【流水账主人】
                        personalMoney.setMoney(wxGroupParticipateMoney);
                        personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-wxGroupParticipateMoney);//收入金额
                    companyAccount.setContent("微信组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+wxGroupParticipateMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }


                //落地组-总负责人id、[json数组]
                Double performGroupMoney = money * leverageConfig.getPerformGroup();
                List<String> performGroupResponsibleList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupResponsible(), String.class);
                Double  performGroupResponsibleMoney =performGroupMoney * leverageConfig.getPerformGroupPrimary() / performGroupResponsibleList.size();
                for (String id : performGroupResponsibleList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(performGroupResponsibleMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("杠杆计划提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                        personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(id);//个人id【流水账主人】
                        personalMoney.setMoney(performGroupResponsibleMoney);
                        personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-performGroupResponsibleMoney);//收入金额
                    companyAccount.setContent("落地组总负责人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupResponsibleMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //落地组-讲师id、[json数组]
                List<String> performGroupLecturerList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupLecturer(), String.class);
                Double  performGroupLecturerMoney =performGroupMoney * leverageConfig.getPerformGroupTeacher() / performGroupLecturerList.size();
                for (String id : performGroupLecturerList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(performGroupLecturerMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(performGroupLecturerMoney);
                            personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-performGroupLecturerMoney);//收入金额
                    companyAccount.setContent("落地组讲师："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupLecturerMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //落地组-参与人id、[json数组]
                List<String> performGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupParticipate(), String.class);
                Double  performGroupParticipateMoney =performGroupMoney * leverageConfig.getPerformGroupSecondary() / performGroupParticipateList.size();
                for (String id : performGroupParticipateList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(performGroupParticipateMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(performGroupParticipateMoney);
                            personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-performGroupParticipateMoney);//收入金额
                    companyAccount.setContent("落地组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupParticipateMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //保障组参与人id、[json数组]
                List<String> securityGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getSecurityGroupParticipate(), String.class);
                Double securityGroupParticipateMoney =money * leverageConfig.getSecurityGroup() / securityGroupParticipateList.size();
                for (String id : securityGroupParticipateList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(securityGroupParticipateMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(securityGroupParticipateMoney);
                            personalMoneyMapper.insert(personalMoney);


                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-securityGroupParticipateMoney);//收入金额
                    companyAccount.setContent("保障组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+securityGroupParticipateMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }





                //完成进度  0 未达到80%   1 80%-100%   2 100%   3 100%+
                leveragePlanInformationMapper.updateCompleteSchedule(leveragePlanInformation.getId(),"2");
                leveragePlanInformationMapper.updateState(leveragePlanInformation.getId(),"1");



                //公司总账减去 本金的50%
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划完成80%~100%，退还本金的50%");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-(leveragePlanInformation.getPrincipal()/2));//减去本金
                companyAccount.setContent("负责员工"+leveragePlanInformation.getEmployeeName()+"客户:"+
                        leveragePlanInformation.getCustomerName()
                        +"杠杆计划:"+leveragePlanInformation.getLeverageTitle()+"未达标退还："+
                        (leveragePlanInformation.getPrincipal()/2));//记账内容
                companyAccountMapper.insert(companyAccount);


                //通知财务一声
                Notice notice = new Notice();
                notice.setContent("客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone()+
                        "   杠杆计划："+leveragePlanInformation.getLeverageTitle()+"未达到80%，需退还客户本金："
                        +(leveragePlanInformation.getPrincipal()/2)+"元");
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId("财务部");
                personalMessage.setReceiverName("财务部");
                personalMessage.setDepartment("财务部");
                personalMessageMapper.insert(personalMessage);



            } else if (percentage == 1){


                //2.查询对应各组的人员，和 对应的 提成比例
                //（本金 - 成本） *  百分比
                Double qualified = leverage.getQualified();
                Double money = (leveragePlanInformation.getPrincipal() - leveragePlanInformation.getCost()) * qualified;
                //原始服务经理id、[json数组]
                List<String> originalManagerList = JsonUtils.parseList(leveragePlanInformation.getOriginalManager(), String.class);
                Double  originalManagerMoney =money * leverageConfig.getOriginalPercent() / originalManagerList.size();
                for (String id : originalManagerList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(originalManagerMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(originalManagerMoney);
                            personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-originalManagerMoney);//收入金额
                    companyAccount.setContent("原始服务经理："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+originalManagerMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //微信组-总负责人id、[json数组]
                Double  wxGroupMoney =money * leverageConfig.getWxGroupPercent();
                List<String> wxGroupResponsibleList = JsonUtils.parseList(leveragePlanInformation.getWxGroupResponsible(), String.class);
                Double  wxGroupResponsibleMoney =wxGroupMoney * leverageConfig.getWxGroupPrimary() / wxGroupResponsibleList.size();
                for (String id : wxGroupResponsibleList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(wxGroupResponsibleMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(wxGroupResponsibleMoney);
                            personalMoneyMapper.insert(personalMoney);


                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-wxGroupResponsibleMoney);//收入金额
                    companyAccount.setContent("微信组总负责人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+wxGroupResponsibleMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }


                //微信组-参与人id、[json数组]
                List<String> wxGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getWxGroupParticipate(), String.class);
                Double  wxGroupParticipateMoney =wxGroupMoney * leverageConfig.getWxGroupSecondary() / wxGroupParticipateList.size();
                for (String id : wxGroupParticipateList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(wxGroupParticipateMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);


                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(wxGroupParticipateMoney);
                            personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-wxGroupParticipateMoney);//收入金额
                    companyAccount.setContent("微信组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+wxGroupParticipateMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }


                //落地组-总负责人id、[json数组]
                Double performGroupMoney = money * leverageConfig.getPerformGroup();
                List<String> performGroupResponsibleList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupResponsible(), String.class);
                Double  performGroupResponsibleMoney =performGroupMoney * leverageConfig.getPerformGroupPrimary() / performGroupResponsibleList.size();
                for (String id : performGroupResponsibleList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(performGroupResponsibleMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(performGroupResponsibleMoney);
                            personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-performGroupResponsibleMoney);//收入金额
                    companyAccount.setContent("落地组总负责人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupResponsibleMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //落地组-讲师id、[json数组]
                List<String> performGroupLecturerList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupLecturer(), String.class);
                Double  performGroupLecturerMoney =performGroupMoney * leverageConfig.getPerformGroupTeacher() / performGroupLecturerList.size();
                for (String id : performGroupLecturerList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(performGroupLecturerMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);


                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(performGroupLecturerMoney);
                            personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-performGroupLecturerMoney);//收入金额
                    companyAccount.setContent("落地组讲师："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupLecturerMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //落地组-参与人id、[json数组]
                List<String> performGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupParticipate(), String.class);
                Double  performGroupParticipateMoney =performGroupMoney * leverageConfig.getPerformGroupSecondary() / performGroupParticipateList.size();
                for (String id : performGroupParticipateList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(performGroupParticipateMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);


                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(performGroupParticipateMoney);
                            personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-performGroupParticipateMoney);//收入金额
                    companyAccount.setContent("落地组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupParticipateMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //保障组参与人id、[json数组]
                List<String> securityGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getSecurityGroupParticipate(), String.class);
                Double securityGroupParticipateMoney =money * leverageConfig.getSecurityGroup() / securityGroupParticipateList.size();
                for (String id : securityGroupParticipateList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(securityGroupParticipateMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);


                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(securityGroupParticipateMoney);
                            personalMoneyMapper.insert(personalMoney);

                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-securityGroupParticipateMoney);//收入金额
                    companyAccount.setContent("保障组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+securityGroupParticipateMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }


                //完成进度  0 未达到80%   1 80%-100%   2 100%   3 100%+
                leveragePlanInformationMapper.updateCompleteSchedule(leveragePlanInformation.getId(),"2");
                leveragePlanInformationMapper.updateState(leveragePlanInformation.getId(),"1");
                //如果正好100% 不做操作
                //通知财务一声
                Notice notice = new Notice();
                notice.setContent("客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone()+
                        "   杠杆计划："+leveragePlanInformation.getLeverageTitle()+"已完成100%");
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId("财务部");
                personalMessage.setReceiverName("财务部");
                personalMessage.setDepartment("财务部");
                personalMessageMapper.insert(personalMessage);


            }else if (percentage > 1){

                //2.查询对应各组的人员，和 对应的 提成比例
                //（（本金 - 成本） *  百分比 ）  +  （ 额外完成 *  比例）
                Double beyond = leverage.getBeyond();//100%+ 比例
                Double excessPercentage = leverage.getExcessPercentage();//额外的比例
                Double moreMoney = leveragePlanInformation.getActualAmount() - leveragePlanInformation.getTargetAmount(); //额外金额
                Double money = ((leveragePlanInformation.getPrincipal() - leveragePlanInformation.getCost()) * beyond) +  (moreMoney * excessPercentage);
                //原始服务经理id、[json数组]
                List<String> originalManagerList = JsonUtils.parseList(leveragePlanInformation.getOriginalManager(), String.class);
                Double  originalManagerMoney =money * leverageConfig.getOriginalPercent() / originalManagerList.size();
                for (String id : originalManagerList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(originalManagerMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(originalManagerMoney);
                            personalMoneyMapper.insert(personalMoney);


                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-originalManagerMoney);//收入金额
                    companyAccount.setContent("原始服务经理："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+originalManagerMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //微信组-总负责人id、[json数组]
                Double  wxGroupMoney =money * leverageConfig.getWxGroupPercent();
                List<String> wxGroupResponsibleList = JsonUtils.parseList(leveragePlanInformation.getWxGroupResponsible(), String.class);
                Double  wxGroupResponsibleMoney =wxGroupMoney * leverageConfig.getWxGroupPrimary() / wxGroupResponsibleList.size();
                for (String id : wxGroupResponsibleList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(wxGroupResponsibleMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);


                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(wxGroupResponsibleMoney);
                            personalMoneyMapper.insert(personalMoney);


                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-wxGroupResponsibleMoney);//收入金额
                    companyAccount.setContent("微信组总负责人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+wxGroupResponsibleMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }


                //微信组-参与人id、[json数组]
                List<String> wxGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getWxGroupParticipate(), String.class);
                Double  wxGroupParticipateMoney =wxGroupMoney * leverageConfig.getWxGroupSecondary() / wxGroupParticipateList.size();
                for (String id : wxGroupParticipateList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(wxGroupParticipateMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(wxGroupParticipateMoney);
                            personalMoneyMapper.insert(personalMoney);


                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-wxGroupParticipateMoney);//收入金额
                    companyAccount.setContent("微信组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+wxGroupParticipateMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }


                //落地组-总负责人id、[json数组]
                Double performGroupMoney = money * leverageConfig.getPerformGroup();
                List<String> performGroupResponsibleList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupResponsible(), String.class);
                Double  performGroupResponsibleMoney =performGroupMoney * leverageConfig.getPerformGroupPrimary() / performGroupResponsibleList.size();
                for (String id : performGroupResponsibleList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(performGroupResponsibleMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(performGroupResponsibleMoney);
                            personalMoneyMapper.insert(personalMoney);


                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-performGroupResponsibleMoney);//收入金额
                    companyAccount.setContent("落地组总负责人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupResponsibleMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //落地组-讲师id、[json数组]
                List<String> performGroupLecturerList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupLecturer(), String.class);
                Double  performGroupLecturerMoney =performGroupMoney * leverageConfig.getPerformGroupTeacher() / performGroupLecturerList.size();
                for (String id : performGroupLecturerList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(performGroupLecturerMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(performGroupLecturerMoney);
                            personalMoneyMapper.insert(personalMoney);


                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-performGroupLecturerMoney);//收入金额
                    companyAccount.setContent("落地组讲师："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupLecturerMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //落地组-参与人id、[json数组]
                List<String> performGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupParticipate(), String.class);
                Double  performGroupParticipateMoney =performGroupMoney * leverageConfig.getPerformGroupSecondary() / performGroupParticipateList.size();
                for (String id : performGroupParticipateList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(performGroupParticipateMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);


                            //存入 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("杠杆计划提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(id);//个人id【流水账主人】
                            personalMoney.setMoney(performGroupParticipateMoney);
                            personalMoneyMapper.insert(personalMoney);


                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-performGroupParticipateMoney);//收入金额
                    companyAccount.setContent("落地组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupParticipateMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }

                //保障组参与人id、[json数组]
                List<String> securityGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getSecurityGroupParticipate(), String.class);
                Double securityGroupParticipateMoney =money * leverageConfig.getSecurityGroup() / securityGroupParticipateList.size();
                for (String id : securityGroupParticipateList) {
                    Employee employee = employeeMapper.selectById(id);
                    //添加员工业绩
                    EmployeePerformance employeePerformance = new EmployeePerformance();
                    employeePerformance.setEmployeeProfit(securityGroupParticipateMoney);//设置员工业绩提成
                    employeePerformance.setEmployeeId(employee.getId());//员工id
                    employeePerformance.setEmployeeName(employee.getName());//员工姓名
                    employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                    employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                    employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                    employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                    employeePerformance.setInputTime(new Date()); //业绩的录入日期
                    employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                    employeePerformanceMapper.insert(employeePerformance);

                                //存入 个人流水账 表
                                PersonalMoney personalMoney = new PersonalMoney();
                                personalMoney.setTitle("杠杆计划提成");
                                personalMoney.setCreateTime(new Date());
                                personalMoney.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone());
                                personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
                                personalMoney.setPersonId(id);//个人id【流水账主人】
                                personalMoney.setMoney(securityGroupParticipateMoney);
                                personalMoneyMapper.insert(personalMoney);


                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount = new CompanyAccount();
                    companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                    companyAccount.setCreateTime(new Date());       //创建时间
                    companyAccount.setMoney(-securityGroupParticipateMoney);//收入金额
                    companyAccount.setContent("保障组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                    companyAccountMapper.insert(companyAccount);

                    //给员工发一条通知
                    Notice notice = new Notice();
                    notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+securityGroupParticipateMoney);
                    noticeMapper.insert(notice);

                    PersonalMessage personalMessage = new PersonalMessage();
                    personalMessage.setMessageType("通知消息");
                    personalMessage.setSenderId("系统");
                    personalMessage.setSenderName("系统");
                    personalMessage.setMessageLink(notice.getId());
                    personalMessage.setCreateTime(new Date());
                    personalMessage.setState("0");//消息状态 0未读
                    personalMessage.setReceiverId(employee.getId());
                    personalMessage.setReceiverName(employee.getName());
                    personalMessageMapper.insert(personalMessage);

                }



                leveragePlanInformationMapper.updateCompleteSchedule(leveragePlanInformation.getId(),"3");
                leveragePlanInformationMapper.updateState(leveragePlanInformation.getId(),"1");
                //高于100% 的金额归公司所有
                //给公司总账，添加一条明细记录
                Double profit = leveragePlanInformation.getActualAmount() - leveragePlanInformation.getTargetAmount();//额外收入
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划超100%完成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(profit);//收入金额
                companyAccount.setContent("杠杆计划额外收入："+profit);//记账内容
                companyAccountMapper.insert(companyAccount);
                //通知财务一声
                //通知财务一声
                Notice notice = new Notice();
                notice.setContent("客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone()+
                        "   杠杆计划："+leveragePlanInformation.getLeverageTitle()+"超额完成100%+   超额 "+profit+" 元");
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId("财务部");
                personalMessage.setReceiverName("财务部");
                personalMessage.setDepartment("财务部");
                personalMessageMapper.insert(personalMessage);
            }


    }


   }


    /**
     * 系统每月 5 号结算上个月 大、小分公司 赠送的课程款，对分公司推广的客户进行结算【把结果通知 发给财务，分公司】
     */
    @Scheduled(cron = "0 0 0 5 * ? ")
    @Transactional
    public void branchCompany(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为上月第一天
        Date startDate = c.getTime();

        Calendar c2 = Calendar.getInstance();
        c2.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        Date endDate = c2.getTime();

        //结算大分公司[通知分公司，通知财务]
        List<BigCompanyRecordDTO> bigCompanyRecords = bigCompanyRecordMapper.queryListByDate(startDate,endDate);
        for (BigCompanyRecordDTO bigCompanyRecord : bigCompanyRecords) {
            //通知财务一声
            Notice notice = new Notice();
            String serialize = JsonUtils.serialize(bigCompanyRecord.getRecords());
            notice.setContent(serialize);//转交的转交 课程 对象【前端慢慢去处理吧】
            noticeMapper.insert(notice);
            PersonalMessage personalMessage = new PersonalMessage();
            personalMessage.setMessageType("分公司结算消息");
            personalMessage.setSenderId("系统");
            personalMessage.setSenderName("系统");
            personalMessage.setMessageLink(notice.getId());
            personalMessage.setCreateTime(new Date());
            personalMessage.setState("0");//消息状态 0未读
            personalMessage.setReceiverId("财务部");
            personalMessage.setReceiverName("财务部");
            personalMessage.setDepartment("财务部");
            personalMessageMapper.insert(personalMessage);
            //通知该公司一声
            PersonalMessage personalMessage2 = new PersonalMessage();
            personalMessage2.setMessageType("分公司结算消息");
            personalMessage2.setSenderId("系统");
            personalMessage2.setSenderName("系统");
            personalMessage2.setMessageLink(notice.getId());
            personalMessage2.setCreateTime(new Date());
            personalMessage2.setState("0");//消息状态 0未读
            personalMessage2.setReceiverId(bigCompanyRecord.getCompanyId());
            Customer company = customerMapper.selectById(bigCompanyRecord.getCompanyId());
            personalMessage2.setReceiverName(company.getName());
            personalMessageMapper.insert(personalMessage2);
        }

        //结算小分公司[通知分公司，通知财务]
        List<SmallCompanyRecordDTO> smallCompanyRecords = smallCompanyRecordMapper.queryListByDate(startDate, endDate);
        for (SmallCompanyRecordDTO smallCompanyRecord : smallCompanyRecords) {

            //通知财务一声
            Notice notice = new Notice();
            String serialize = JsonUtils.serialize(smallCompanyRecord.getRecords());
            notice.setContent(serialize);//转交的转交 课程 对象【前端慢慢去处理吧】
            noticeMapper.insert(notice);
            PersonalMessage personalMessage = new PersonalMessage();
            personalMessage.setMessageType("分公司结算消息");
            personalMessage.setSenderId("系统");
            personalMessage.setSenderName("系统");
            personalMessage.setMessageLink(notice.getId());
            personalMessage.setCreateTime(new Date());
            personalMessage.setState("0");//消息状态 0未读
            personalMessage.setReceiverId("财务部");
            personalMessage.setReceiverName("财务部");
            personalMessage.setDepartment("财务部");
            personalMessageMapper.insert(personalMessage);
            //通知该公司一声
            PersonalMessage personalMessage2 = new PersonalMessage();
            personalMessage2.setMessageType("分公司结算消息");
            personalMessage2.setSenderId("系统");
            personalMessage2.setSenderName("系统");
            personalMessage2.setMessageLink(notice.getId());
            personalMessage2.setCreateTime(new Date());
            personalMessage2.setState("0");//消息状态 0未读
            personalMessage2.setReceiverId(smallCompanyRecord.getCompanyId());
            Customer company = customerMapper.selectById(smallCompanyRecord.getCompanyId());
            personalMessage2.setReceiverName(company.getName());
            personalMessageMapper.insert(personalMessage2);

        }
    }





}
